package com.phone580.app.untilty;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;

public class ImageCache {
	
	public static void saveImage(final String url, final Context context) {
		Thread t = new Thread() {
			@Override
			public void run() {
				try {
					byte[] b = downloadFile(url);
					CommonUtility.storeIcon(context, url, b);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			private byte[] downloadFile(String url) throws IOException {
				URL httpurl = new URL(url);
				HttpURLConnection connection = (HttpURLConnection)httpurl.openConnection();
				byte[] b = new byte[4096];
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				int len = 0;
				InputStream in = connection.getInputStream();
				while ((len = in.read(b, 0, b.length)) != -1) {
					bos.write(b, 0, len);
				}
				connection.disconnect();
				byte[] data = bos.toByteArray();
				bos.close();
				return data;
			}
		};
		t.start();
	}
	
}
